#!/usr/bin/env python

from win32com.client import Dispatch
import pepe.datetimex.basic as pdtb
import string
import re

slj = lambda x, y: string.ljust(str(x), y)
# mm/dd/yy -> yyyy-mm-dd
isoymd = lambda x:  pdtb.toIso(str(x)[:8], timeStampType='MM/DD/YY') + str(x)[8:]

session = Dispatch("MAPI.session")
session.Logon('Deloitte Profile')  # MAPI profile name
inbox = session.Inbox

#print "Inbox name is:", inbox.Name
#print "Number of messages:", inbox.Messages.Count
#print ' ID/StoreID:',message.ID,'/',message.StoreID
#print message.Submitted

print string.join(['Sent','Received','Size',\
                   'Importance','AttchCount',\
                   'Sender','Subject','Recipients',\
                   'AttchNames'],'~')

for i in range(inbox.Messages.Count):
    message = inbox.Messages.Item(i + 1)

    out = []
    #print slj(' #Sent#',20), \
    out.append(isoymd(str(message.TimeSent)))

    #print slj(' #Received#',20), \
    out.append(isoymd(str(message.TimeReceived)))

    #print slj(' #Size#',20), \
    out.append(str(message.Size))

    #print slj(' #Importance#',20), \
    out.append(str(message.Importance))

    #print slj(' #Attachments.Count:',20),\
    out.append(str(message.Attachments.Count))

    #print slj(' #Sender#',20), \
    out.append(str(message.Sender))

    #print slj(' #Subject# ',20),\
    out.append(re.sub('~','_',message.Subject))

    #print message.Recipients.Count
    #print slj(' #Recipients#',20),
    recipients = []
    for x in message.Recipients:
        #print x.Address
        recipients.append(str(x.AddressEntry))
    out.append(string.join(recipients,';'))
    #if message.Attachments.Count <> 0:
    #print slj(' #Attachments.Names:',20)
    attachments = []
    for x in message.Attachments:
        #print x.Address
        attachments.append('\"'+str(x.Name)+'\"')
    out.append(string.join(attachments,';'))
    #print 'Text:', message.Text
    #print '\n###'
    print string.join(out,'~')
